# asterisk-siptrunks.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

# This file is part of the CarrierClass.net project.
# For more information: http://carrierclass.net/
#
package CCNv2::Provision::Asterisk::siptrunks;
use strict;
use warnings;
use base qw(CCNCore::Provision::Base);
use CCN::Text;
use CCNCore::Scripting;

sub run()
{
    my $self = shift;
    return unless defined $self->p->{cp_member};

    my $siprouteconf = '';
    my $siptrunkconf = '';
    my $register = '';

    $self->sql_do_for( <<'SQL', $self->p->{cp_member},
        SELECT pk, sip_type, sip_username, sip_password, sip_proxy
        FROM trunk WHERE type = 'sip' AND server = ?;
SQL
    sub
    {
        my ($TRUNK,$TYPE,$USERNAME,$PASSWORD,$PROXY) = @_;
        # Inbound routing
        $siprouteconf .= $self->ast_route_trunk_outbound(${TRUNK});

        # Outbound routing
        $siprouteconf .= $self->ast_route_trunk_inbound(${TRUNK});
        $siprouteconf .= 
            "[dial-trunk-$TRUNK]\n".
            "exten => _X.,1,Dial(SIP/trunk-$TRUNK/\${EXTEN})\n";

        # SIP-specific parameters
        $siptrunkconf .= "[trunk-${TRUNK}]\n";

        # A SIP trunk that handles CBeyond
        if( ${TYPE} eq 'cbeyond' )
        {
              $register .= <<"EOT";
register => ${USERNAME}:${PASSWORD}:${USERNAME}\@${PROXY}/${USERNAME}
EOT
              $siptrunkconf .= <<"EOT";
username=${USERNAME}
type=friend
secret=${PASSWORD}
qualify=1000
insecure=very
host=${PROXY}
fromdomain=${PROXY}
; As of 2006/10, CBeyond does not provide RFC2833 inbound,
; which makes them unsuitable for IVR applications.
; This setting will provide RFC2833 outbound and inband
; detection inbound to workaround the limitation.
dtmfmode=auto
context=from-gateway-${TRUNK}
accountcode=${TRUNK}
canreinvite=no
disallow=all
allow=ulaw
regseconds=60
EOT
        }

        # A SIP trunk that handles gafachi
        if( ${TYPE} eq 'gafachi' )
        {
              $register .= <<"EOT";
register => ${USERNAME}:${PASSWORD}\@${USERNAME}.sip.gafachi.com
EOT
              $siptrunkconf .= <<"EOT";
username=${USERNAME}
type=friend
secret=${PASSWORD}
qualify=1000
insecure=very
host=${USERNAME}.sip.gafachi.com
;fromdomain=${USERNAME}.sip.gafachi.com
fromuser=${USERNAME}
dtmfmode=rfc2833
context=from-gateway-${TRUNK}
accountcode=${TRUNK}
canreinvite=no
disallow=all
allow=ulaw
allow=alaw
allow=g729
regseconds=60
EOT
        }

        # A SIP trunk that accepts E.164 numbers
        if( ${TYPE} eq 'e164' )
        {
              $register .= <<"EOT";
register => ${USERNAME}:${PASSWORD}\@${PROXY}
EOT
              $siptrunkconf .= <<"EOT";
username=${USERNAME}
type=friend
secret=${PASSWORD}
qualify=1000
insecure=very
host=${PROXY}
fromdomain=${PROXY}
; nat=always
;dtmfmode=rfc2833
dtmfmode=auto
context=from-gateway-${TRUNK}
accountcode=${TRUNK}
canreinvite=no
disallow=all
allow=ulaw
regseconds=60
EOT
        }
    }); # foreach @siptrunks

    foreach('/etc/asterisk/config/sip.siptrunks.conf')
    {
        CAT $_, $siptrunkconf;
        IF_CHANGED $_ => CCNv2::Scripting::AST('sip reload');
    }
    foreach('/etc/asterisk/config/sip.siptrunks.register.conf')
    {
        CAT $_, $register;
        IF_CHANGED $_ => CCNv2::Scripting::AST('sip reload');
    }

    foreach('/etc/asterisk/config/extensions.siptrunks.conf')
    {
        CAT $_, $siprouteconf;
        IF_CHANGED $_ => CCNv2::Scripting::AST('extensions reload');
    }
}
1;